<template>
  <div class="session-list-container">
    <h1>会话审计</h1>
    
    <el-card style="margin-bottom: 20px;">
      <el-form :model="searchForm" inline>
        <el-form-item label="用户">
          <el-input v-model="searchForm.user" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="主机">
          <el-input v-model="searchForm.host" placeholder="请输入主机名" clearable />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker v-model="searchForm.start_date" type="datetime" placeholder="选择开始时间" />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker v-model="searchForm.end_date" type="datetime" placeholder="选择结束时间" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table :data="sessions" style="width: 100%" v-loading="loading">
      <el-table-column prop="session_id" label="会话ID" width="120">
        <template #default="scope">
          <span>{{ scope.row.session_id?.substring(0, 8) + '...' || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="user_name" label="用户" min-width="100" />
      <el-table-column prop="host_name" label="主机" min-width="120" />
      <el-table-column prop="host_ip" label="IP地址" min-width="120" />
      <el-table-column prop="start_time" label="开始时间" min-width="160" />
      <el-table-column prop="end_time" label="结束时间" min-width="160">
        <template #default="scope">
          {{ scope.row.end_time || '进行中' }}
        </template>
      </el-table-column>
      <el-table-column label="持续时间" min-width="100">
        <template #default="scope">
          {{ formatDuration(scope.row.start_time, scope.row.end_time) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'" size="small">
            {{ scope.row.status === 'active' ? '活跃' : '已结束' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" type="primary" @click="viewCommands(scope.row)">
            查看命令
          </el-button>
          <el-button size="small" type="success" @click="replaySession(scope.row)">
            回放
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="pagination.current"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handlePageChange"
      @size-change="handlePageChange"
      style="margin-top: 20px; text-align: right;"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const sessions = ref([])
const loading = ref(false)
const searchForm = ref({
  user: '',
  host: '',
  start_date: '',
  end_date: ''
})
const pagination = ref({
  current: 1,
  pageSize: 20,
  total: 0
})

const fetchSessions = async () => {
  loading.value = true
  try {
    // 模拟数据，实际应该调用API
    sessions.value = [
      {
        session_id: 'sess_123456789',
        user_name: 'admin',
        host_name: 'web-server-01',
        host_ip: '192.168.1.101',
        start_time: '2023-12-01 10:30:00',
        end_time: '2023-12-01 11:15:00',
        status: 'closed'
      },
      {
        session_id: 'sess_987654321',
        user_name: 'user1',
        host_name: 'db-server-01',
        host_ip: '192.168.1.103',
        start_time: '2023-12-01 09:15:00',
        end_time: null,
        status: 'active'
      }
    ]
    pagination.value.total = sessions.value.length
  } catch (error) {
    console.error('获取会话列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.value.current = 1
  fetchSessions()
}

const handleReset = () => {
  searchForm.value = {
    user: '',
    host: '',
    start_date: '',
    end_date: ''
  }
  handleSearch()
}

const handlePageChange = () => {
  fetchSessions()
}

const viewCommands = (session: any) => {
  router.push(`/audit/commands?session_id=${session.session_id}`)
}

const replaySession = (session: any) => {
  if (session.session_id) {
    router.push(`/audit/replay/${session.session_id}`)
  } else {
    ElMessage.warning('该会话没有可用的回放数据')
  }
}

const formatDuration = (start: string, end: string | null) => {
  if (!end) return '进行中'
  const duration = new Date(end).getTime() - new Date(start).getTime()
  const hours = Math.floor(duration / (1000 * 60 * 60))
  const minutes = Math.floor((duration % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = Math.floor((duration % (1000 * 60)) / 1000)
  return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
}

onMounted(() => {
  fetchSessions()
})
</script>

<style lang="scss" scoped>
.session-list-container {
  padding: 20px;
}

h1 {
  color: #00eaff;
  margin-bottom: 20px;
  text-shadow: 0 0 10px rgba(0, 234, 255, 0.5);
}
</style>
